<--- %%NOBANNER%% --> _suffix.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| List a list of variables using a suffix;                           |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Arguments:                                                         |
|    _suffix_ : a suffix, required;                                  |
|    _list_   : a list of prefix; or some looping command, such as   |
|               1 to 10 by 3 or 1 to 10 etc.                         |
|---------------<-- End of Files Arguments Needed-->-----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %put %_suffix(a b as, this);                              |
|          %put %_suffix(2 to 10, this);                             | 
| Usage:   %_suffix(_suffix_, _pre_list_)                            |
\-------------------<-- End of Files Created-->---------------------*/
%macro _suffix (_pre_list_, _suffix_) /des='adds suffix to list of words' ;
%if (%indexw(%nrbquote(%upcase(%nrbquote(&_pre_list_))), %nrbquote(TO))) and 
    (((%words(%nrbquote(%upcase(%nrbquote(&_pre_list_))), dlm=%nrbquote(TO BY%%)) eq 1) and  (%sysfunc(rxmatch(%sysfunc(rxparse($d*)),%nrbquote(%upcase(%nrbquote(&_pre_list_)))))))  or 
     ((%words(%nrbquote(%upcase(%nrbquote(&_pre_list_))), dlm=%nrbquote(TO BY%%)) eq 2) and  (%sysfunc(rxmatch(%sysfunc(rxparse($d* [' '] (TO) [' '] $d*)),%nrbquote(%upcase(%nrbquote(&_pre_list_))))))) or 
     ((%words(%nrbquote(%upcase(%nrbquote(&_pre_list_))), dlm=%nrbquote(TO BY%%)) eq 3) and  (%sysfunc(rxmatch(%sysfunc(rxparse($d* [' '] (TO) [' '] $d* [' '] (BY) [' '] $d*)),%nrbquote(%upcase(%nrbquote(&_pre_list_)))))))) 
   %then %do;
   %let _suffixfrom_=; %let _suffixto_=; %let _suffixby_=1;
   %do _suffixi_ = 1 %to %words(%nrbquote(%upcase(%nrbquote(&_pre_list_))), dlm=%nrbquote(TO BY%%));
      %if (&_suffixi_=1) %then 
         %let _suffixfrom_=%qscan(%nrbquote(%upcase(%nrbquote(&_pre_list_))), &_suffixi_, %nrbquote(TO BY%%));
      %else %if (&_suffixi_=2) %then 
         %let _suffixto_=%qscan(%nrbquote(%upcase(%nrbquote(&_pre_list_))), &_suffixi_, %nrbquote(TO BY%%));
      %else %if (&_suffixi_=3) %then 
         %let _suffixby_=%qscan(%nrbquote(%upcase(%nrbquote(&_pre_list_))), &_suffixi_, %nrbquote(TO BY%%));
   %end;
   %if (%chk_type(&_suffixfrom_) eq 1) and (%chk_type(&_suffixto_) ne 1) and (%quote(&_suffixto_) ne) %then 
      %let _suffixto_=&_suffixfrom_;
   %else %if (%chk_type(&_suffixfrom_) ne 1) and (%chk_type(&_suffixto_) eq 1) and (%quote(&_suffixfrom_) ne) %then 
      %let _suffixfrom_=&_suffixto_;
   %if (%chk_type(&_suffixfrom_) eq 1) and (%chk_type(&_suffixto_) eq 1) and (%chk_type(&_suffixby_) ne 1) and (%quote(&_suffixby_) ne) %then
      %let _suffixby_=1;
   %let _suffixout_=;
   %if (%chk_type(&_suffixfrom_) ne 1) or (%chk_type(&_suffixto_) ne 1) or (%chk_type(&_suffixby_) ne 1) %then %do;
      %put ==> Alert! Invalid Input: at least one of from, to or by is a character variable!;
      %goto finish;
   %end; 
   %do _suffixi_=&_suffixfrom_ %to &_suffixto_ %by &_suffixby_;
      %let _suffixout_= &_suffixout_%str( )%trim(%left(&_suffixi_))%trim(%left(&_suffix_));
   %end; &_suffixout_
%end;
%else %do;
   %let _suffixout_ = %str() ;
   %do _suffixi_ = 1 %to %words(%nrbquote(&_pre_list_));
      %let _suffixout_ = &_suffixout_%str( )%trim(%left(%qscan(%nrbquote(&_pre_list_), &_suffixi_)))%trim(%left(&_suffix_));
   %end ;&_suffixout_
%end;
%finish:
%mend _suffix ;